package vip.xiaonuo.modular.file.util;

import java.io.*;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * Description： 文件压缩与解压
 * Author: chenyou
 * Date: 2022/4/7 13:51
 * Company: ttg
 * Copyright: Copyright (c) 2021
 */
public class ZipUtil {
    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

    /**
     * zip压缩
     * 压缩多个文件到一个zip文件中
     *
     * @param files       待压缩文件数组
     * @param zipFilePath 压缩后的zip文件路径 "D:\\cloudpay\\billtips"
     * @param fileName    zip文件的名字 "001"
     * @return 是否成功
     */
    public static boolean fileToZip(File[] files, String zipFilePath, String fileName) {
        boolean flag = false;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;

        try {
            File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
            if (zipFile.exists()) {
                zipFile.delete();
            }
            File[] sourceFiles = files;
            fos = new FileOutputStream(zipFile);
            zos = new ZipOutputStream(new BufferedOutputStream(fos));
            byte[] bufs = new byte[1024 * 10];
            for (int i = 0; i < sourceFiles.length; i++) {
                //创建ZIP实体，并添加进压缩包
                ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                zos.putNextEntry(zipEntry);
                //读取待压缩的文件并写进压缩包里
                fis = new FileInputStream(sourceFiles[i]);
                bis = new BufferedInputStream(fis, 1024 * 10);
                int read = 0;
                while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                    zos.write(bufs, 0, read);
                }
            }
            flag = true;


        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            //关闭流
            try {
                if (null != bis) bis.close();
                if (null != zos) zos.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }

        return flag;
    }

    /**
     * zip解压
     *
     * @param zipFilePath   待解压文件的路径
     * @param unzipFilePath 解压后的文件存储路径
     * @throws Exception
     */
    public static void unzip(String zipFilePath, String unzipFilePath) {
        File zipFile = new File(zipFilePath);


        //创建解压缩文件保存的路径
        File unzipFileDir = new File(unzipFilePath);
        if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) {
            unzipFileDir.mkdirs();
        }

        //开始解压
        ZipEntry entry = null;
        String entryFilePath = null, entryDirPath = null;
        File entryFile = null, entryDir = null;
        int index = 0, count = 0;
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
            Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
            //循环对压缩包里的每一个文件进行解压
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                //构建压缩包中一个文件解压后保存的文件全路径
                entryFilePath = unzipFilePath + File.separator + entry.getName();
                //构建解压后保存的文件夹路径
                index = entryFilePath.lastIndexOf(File.separator);
                if (index != -1) {
                    entryDirPath = entryFilePath.substring(0, index);
                } else {
                    entryDirPath = "";
                }
                entryDir = new File(entryDirPath);
                //如果文件夹路径不存在，则创建文件夹
                if (!entryDir.exists() || !entryDir.isDirectory()) {
                    entryDir.mkdirs();
                }

                //创建解压文件
                entryFile = new File(entryFilePath);
                if (entryFile.exists()) {
                    //检测文件是否允许删除，如果不允许删除，将会抛出SecurityException
                    SecurityManager securityManager = new SecurityManager();
                    securityManager.checkDelete(entryFilePath);
                    //删除已存在的目标文件
                    entryFile.delete();
                }

                //写入文件
                bos = new BufferedOutputStream(new FileOutputStream(entryFile));
                bis = new BufferedInputStream(zip.getInputStream(entry));
                while ((count = bis.read(buffer, 0, 1024)) != -1) {
                    bos.write(buffer, 0, count);
                }
                bos.flush();
                bos.close();
            }
            zip.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }


    public static void main(String[] args) throws IOException {
//        File[] files = new File[2];
//        File file1 = new File("D:\\cloudpay\\billtips\\001.csv");
//        File file2 = new File("D:\\cloudpay\\billtips\\002.csv");
//        files[0] = file1;
//        files[1] = file2;
//        String zipFilePath = "D:\\cloudpay\\billtips";
//        String zipFileName = "001";

        String zipFilePath1 = "D:\\ALL\\内网通1\\文件管理\\CY-HP\\2022\\2022格网化指标\\格网化指标_武汉.zip";
        String unzipFilePath = "D:\\ALL\\内网通1\\文件管理\\CY-HP\\2022\\2022格网化指标\\1";
        File file=new File(zipFilePath1);
        if (file.getName().contains("zip")) {
            System.out.println(file.getName());
        }

//        fileToZip(files,zipFilePath,zipFileName);
//        unzip(zipFilePath1,unzipFilePath);


    }
}
